package com.wtwd.campus.receiver;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.annotation.MsgType;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.constant.UploadAppFileTypeEnum;
import com.wtwd.campus.controller.CustomerController;
import com.wtwd.campus.entity.*;
import com.wtwd.campus.service.DeviceAppPublicFileService;
import com.wtwd.campus.service.DeviceInfoService;
import com.wtwd.campus.service.IRMFeignClientService;
import com.wtwd.campus.utils.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author mjy
 * @date 2022/12/12
 * @description
 */
@Service
@MsgType(value = Constants.UPLOAD_APP_FILE)
public class UploadAppFileHandler implements IReceiverHandler {

    private static final Logger logger = LoggerFactory.getLogger(UploadAppFileHandler.class);

    @Resource
    private IRMFeignClientService irmFeignClientService;

    @Resource
    private DeviceInfoService deviceInfoService;

    @Resource
    private DeviceAppPublicFileService deviceAppPublicFileService;


    private static ExecutorService threadPool = new ThreadPoolExecutor(1,
            5,
            1L,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(4),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy());

    @Override
    public void handlerMessage(MqMessage mqMessage) {
        MqUplocadAppFile uploadAppFile = (MqUplocadAppFile) mqMessage.getObject();
        String imei = uploadAppFile.getImei();
        String commandNumber = uploadAppFile.getCommandNumber();
        //处理指令上报的时间
        String date = uploadAppFile.getDeviceDate();
        String time1 = uploadAppFile.getDeviceTime();
        Long uploadTime = getTime(date, time1);
        DeviceInfo deviceInfo = deviceInfoService.getDeviceByImei(imei);
        if (deviceInfo != null) {
            List<MqAppFile> appFiles = uploadAppFile.getAppFiles();
            if (appFiles != null && appFiles.size() > 0) {//进行文件存储操作和相应业务处理
                threadPool.submit(() -> {
                    logger.info("当前线程{} 开始执行设备app文件上传 param={} ", Thread.currentThread().getName(), appFiles.toString());
                    try {
                        deviceAppPublicFileService.bachCacheSaveFile(uploadAppFile,uploadTime);
                    } catch (Exception e) {
                        logger.info("当前线程{} 执行设备app文件上传异常 异常信息 ", Thread.currentThread().getName(), e.getMessage());
                        e.printStackTrace();
                    }
                });
            }
            MqGeneralReply mqGeneralReply = new MqGeneralReply();
            mqGeneralReply.setImei(imei);
            mqGeneralReply.setCommandNumber(commandNumber);
            //确认收到回复
            irmFeignClientService.replyUploadAppFile(mqGeneralReply);
            deviceInfo.setLastUpTime(System.currentTimeMillis());
            deviceInfoService.modifyDeviceInfo(deviceInfo);
        }


    }
}
